<template>
  <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="130px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item label="学生姓名" prop="studentName">
            <el-input v-model="form.studentName" disabled placeholder="请输入学生姓名" />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="手机号" prop="phoneNumber">
            <el-input v-model="form.phoneNumber" disabled placeholder="请输入手机号" />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="班级名称" prop="className">
            <el-input v-model="form.className" disabled placeholder="请输入班级名称" />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="是否就业" prop="employmentStatus">
            <el-select v-model="form.employmentStatus" placeholder="请选择是否就业"
              @change="selectArrears(form.employmentStatus)">
              <el-option :value="item.value" :label="item.label" v-for="(item, index) in employment_status"
                :key="index"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="入职时间1" prop="entryDate">
            <el-date-picker type="date" placeholder="请选择入职时间" v-model="form.entryDate"
              :value-format="dateStr"></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="离职时间1" prop="departureDate">
            <el-date-picker type="date" placeholder="请选择离职时间" v-model="form.departureDate"
              :value-format="dateStr"></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="工作企业1" prop="company">
            <el-input v-model="form.company" placeholder="请输入工作企业" />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="工作时长1" prop="workDuration">
            <el-input v-model="form.workDuration" placeholder="请输入工作时长" />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="入职时间2" prop="entryDate">
            <el-date-picker type="date" placeholder="请选择入职时间" v-model="form.entryDate2"
              :value-format="dateStr"></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="离职时间2" prop="departureDate">
            <el-date-picker type="date" placeholder="请选择离职时间" v-model="form.departureDate2"
              :value-format="dateStr"></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="工作企业2" prop="company">
            <el-input v-model="form.company2" placeholder="请输入工作企业" />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="工作时长2" prop="workDuration">
            <el-input v-model="form.workDuration2" placeholder="请输入工作时长" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="就业协议上传路径" prop="employmentAgreement">
            <upload-file v-model="form.employmentAgreement"></upload-file>
            <!-- <upload-img v-model:imageUrl="form.employmentAgreement"></upload-img> -->
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="StudentEmploymentDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj, validateExist } from '/@/api/admin/studentEmployment'
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典
const { employment_status } = useDict('employment_status')

// 提交表单数据
const form = reactive({
  id: '',
  studentName: '',
  phoneNumber: '',
  className: '',
  employmentStatus: '',
  entryDate: '',
  departureDate: '',
  company: '',
  workDuration: '',
  employmentAgreement: '',
  remark: '',
});

// 定义校验规则
const dataRules = ref({
  studentName: [{ required: true, message: '学生姓名不能为空', trigger: 'blur' }],
  phoneNumber: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, { validator: rule.mobilePhone, trigger: 'blur' }],
  className: [{ required: true, message: '班级名称不能为空', trigger: 'blur' }],
  employmentStatus: [{ required: true, message: '是否就业不能为空', trigger: 'blur' }],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取studentEmployment信息
  if (id) {
    form.id = id
    getStudentEmploymentData(id)
  }
};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => { });
  if (!valid) return false;

  try {
    loading.value = true;
    if (!form.entryDate) {
      delete form.entryDate
    }
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? '修改成功' : '添加成功');
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getStudentEmploymentData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj({ id: id }).then((res: any) => {
    Object.assign(form, res.data[0])
    form.employmentStatus = formatstatus(form.employmentStatus)
  }).finally(() => {
    loading.value = false
  })
};
const formatstatus = (num) => {
  if (num || num == 0) {
    let num2 = num + ''
    return num2
  } else {
    return ''
  }
}
const selectArrears = (id) => {
  console.log(id);
  if (id == 0) {
    form.entryDate = ''
    form.departureDate = ''
    form.company = ''
    form.workDuration = ''
    form.entryDate2 = ''
    form.departureDate2 = ''
    form.company2 = ''
    form.workDuration2 = ''
    console.log("qing")
  }
}
// 暴露变量
defineExpose({
  openDialog
});
</script>
